package io.renren.modules.generator.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.common.utils.excel.ExcelUtils;
import io.renren.modules.generator.entity.DictionaryEntity;
import io.renren.modules.generator.export.DictionartExcelDataCreaterImpl;
import io.renren.modules.generator.service.DictionaryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-11-26 15:10:03
 */
@RestController
@RequestMapping("generator/dictionary")
public class DictionaryController {
    @Autowired
    private DictionaryService dictionaryService;

    @Autowired
    private ExcelUtils excelUtils;

    @Autowired
    private DictionartExcelDataCreaterImpl dictionartExcelDataCreater;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        Query query = new Query(params);

        List<DictionaryEntity> list = dictionaryService.queryPage(query);

        int total =dictionaryService.queryPageTotal(params);

        PageUtils pageUtils = new PageUtils(list, total, query.getLimit(), query.getCurrPage());

        return R.ok().put("page", pageUtils);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
			DictionaryEntity dictionary = dictionaryService.selectById(id);

        return R.ok().put("dictionary", dictionary);
    }

    /**
     * 保存
     */
    @CacheEvict(value = {"dictionaryController:queryByType"},allEntries = true)
    @RequestMapping("/save")
    @RequiresPermissions("generator:dictionary:save")
    public R save(@RequestBody DictionaryEntity dictionary){
			dictionaryService.insert(dictionary);

        return R.ok();
    }

    /**
     * 修改
     */
    @CacheEvict(value = {"dictionaryController:queryByType"},allEntries = true)
    @RequestMapping("/update")
    @RequiresPermissions("generator:dictionary:update")
    public R update(@RequestBody DictionaryEntity dictionary){
			dictionaryService.updateById(dictionary);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:dictionary:delete")
    public R delete(@RequestBody Integer[] ids){
			dictionaryService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }

    @PostMapping("/queryByType")
    @Cacheable("dictionaryController:queryByType")
    public R queryByType(@RequestBody String[] types){
        List<DictionaryEntity> entities = dictionaryService.queryByType(types);
        Map<String, List<DictionaryEntity>> collect = entities.stream().collect(Collectors.groupingBy(DictionaryEntity::getType));

        return R.ok().put("data",collect);
    }

    /**
     * 更新状态
     * @param ids
     * @return
     */
    @CacheEvict(value = {"dictionaryController:queryByType"},allEntries = true)
    @RequestMapping("/updateStatusByIds")
    @RequiresPermissions("generator:dictionary:status")
    public R updateStatusByIds(@RequestBody Integer[] ids){
        dictionaryService.updateStatusByIds(Arrays.asList(ids));
        return R.ok();
    }

    @GetMapping("export")
    @RequiresPermissions("generator:dictionary:export")
    public void exportDate(@RequestParam Map<String, Object> params, HttpServletRequest request,
                        HttpServletResponse response) throws Exception {
        String[] lableNames = {"ID","类型","代码","名称","状态","顺序","备注"};
        String[] columnNames={"id","type","code","name","status","orderNumber","remark"};
        excelUtils.export(response,"数据导出.xlsx",lableNames,columnNames,params,dictionartExcelDataCreater);

    }
    @Cacheable("dictionaryController:queryAllTypeList")
    @GetMapping("/queryAllTypeList")
    public R queryAllTypeList(){
        List<String> typeList =dictionaryService.queryAllTypeList();
        return R.ok().put("data",typeList);
    }



}
